package part18;

import java.io.*;
import java.nio.IntBuffer;
import java.nio.channels.FileChannel;

/**
 * @author wcj
 * @Date 2021/6/4 16:33
 * @Version 1.0
 */
public class Part1828 {

    private static int numOfInts = 4000000;
    private static int numOfUbuffInts = 200000;

    private abstract static class Tester {
        private String name;

        public Tester(String name) {
            this.name = name;
        }

        public void runTest() {
            System.out.println(name + " ");
            try {
                long start = System.nanoTime();
                test();
                double duration = System.nanoTime() - start;
                System.out.format("%.2f\n", duration / 1.0e9);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        /**
         * test
         *
         * @throws IOException
         */
        public abstract void test() throws IOException;
    }

    private static Tester[] tests = {
            new Tester("Stream Write") {
                @Override
                public void test() throws IOException {
                    DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(new File("temp.tmp"))));
                    for (int i = 0; i < numOfInts; i++) {
                        out.writeInt(i);
                    }
                    out.close();
                }
            },
            new Tester("Stream Read") {
                @Override
                public void test() throws IOException {
                    DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(new File("temp.tmp"))));
                    for (int i = 0; i < numOfInts; i++) {
                        in.readInt();
                    }
                    in.close();
                }
            },
            new Tester("Mapped Read") {
                @Override
                public void test() throws IOException {
                    FileChannel fc = new FileInputStream(new File("temp.tmp")).getChannel();
                    IntBuffer intBuffer = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()).asIntBuffer();
                    while (intBuffer.hasRemaining()) {
                        intBuffer.get();
                    }
                    fc.close();
                }
            },
            new Tester("Stream Read/Write") {
                @Override
                public void test() throws IOException {
                    RandomAccessFile rw = new RandomAccessFile(new File("temp.tmp"), "rw");
                    rw.writeInt(1);
                    for (int i = 0; i < numOfUbuffInts; i++) {
                        rw.seek(rw.length() - 4);
                        rw.writeInt(rw.readInt());
                    }
                    rw.close();
                }
            },
            new Tester("Mapped Read/Write") {
                @Override
                public void test() throws IOException {
                    FileChannel rw = new RandomAccessFile(new File("temp.tmp"), "rw").getChannel();
                    IntBuffer intBuffer = rw.map(FileChannel.MapMode.READ_WRITE, 0, rw.size()).asIntBuffer();
                    intBuffer.put(0);
                    for (int i = 1; i < numOfUbuffInts; i++) {
                        intBuffer.put(intBuffer.get(i - 1));
                    }
                    rw.close();
                }
            }
    };

    /**
     * 映射文件访问的性能比较，这里比较了：
     * FileInputStream,FileOutputStream,RandomAccessFile进行文件的读写操作
     * 以及通过RandomAccessFile.map获取MappedByteBuffer进行的文件读写操作的性能
     * 发现对于映射文件的访问，MappedByteBuffer的性能很高
     *
     * @param args
     */
    public static void main(String[] args) {
        for (Tester test : tests) {
            test.runTest();
        }
    }
}
